#include "LoadMenuState.h"
#include "MainMenuState.h"
#include "OptionsMenuState.h"
#include "SinglePlayerState.h"
#include "CSGD_DirectInput.h"
#include "CSGD_TextureManager.h"
#include "CSGD_XAudio2.h"
#include "LevelManager.h"
#include "Game.h"
#include "BitmapFont.h"
#include "../TinyXML/tinyxml.h"
#include "PauseMenuState.h"
#include "SaveSystem.h"
#include <iostream>
#include <sstream>
#include "EntityManager.h"
#include "../resource.h"

using namespace std;

/////////////////////////////////////////////////////////////////////////////
// Function		: CHowToPlayMenuState
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
CLoadMenuState::CLoadMenuState(void)
{
	m_nBackgroundImg = -1;


}
/////////////////////////////////////////////////////////////////////////////
// Function		: ~CHowToPlayMenuState
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
CLoadMenuState::~CLoadMenuState(void)
{
}
/////////////////////////////////////////////////////////////////////////////
// Function		: GetInstance
// Return		: CHowToPlayMenuState*
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
CLoadMenuState* CLoadMenuState::GetInstance( void )
{
	static CLoadMenuState s_Instance;

	return &s_Instance;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Enter
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CLoadMenuState::Enter( void )
{
	m_nBackgroundImg = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/MenuBackground.png"));

	if(COptionsMenuState::GetInstance()->ReturnEng())
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Slot, Slot, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Time, Time, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_EscToMain, EscToMain, 50);
	}
	else
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Slot, Slot, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Time, Time, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_EscToMain, EscToMain, 50);
	}

	TrueSlot = "";
	TrueEscTMain = "";

	for (int character = 0; character < 20; character++)
	{
		if(Slot[character] != NULL)
			TrueSlot += (char)Slot[character];
		else
			break;
	}

	for (int character = 0; character < 50; character++)
	{
		if(EscToMain[character] != NULL)
			TrueEscTMain += (char)EscToMain[character];
		else
			break;
	}
	for (int i = 0; i < 3; i++)
	{
		m_TimeArr[i] = 0;

		TiXmlDocument doc;

		string in = "SaveSlot";
		ostringstream slotnum;
		slotnum << i+1;
		in += slotnum.str();
		in += ".xml";

		if(doc.LoadFile(in.c_str()) == false)
			continue;

		TiXmlElement* pRoot = doc.RootElement();

		TiXmlElement* pTime = pRoot->FirstChildElement("Time");
		pTime->Attribute("time", &m_TimeArr[i]);
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Exit
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CLoadMenuState::Exit ( void )
{
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBackgroundImg);
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Input
// Return		: bool
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
bool CLoadMenuState::Input( void )
{
	CSGD_DirectInput* m_pDI = CSGD_DirectInput::GetInstance();

	if((m_pDI->KeyPressed(DIK_UP)  || m_pDI->JoystickDPadPressed( DIR_UP ) == true) && m_nCursor > 0)
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetMenuMovID());
		m_nCursor--;
	}

	else if((m_pDI->KeyPressed(DIK_DOWN) || m_pDI->JoystickDPadPressed( DIR_DOWN ) == true) && m_nCursor < 2)
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetMenuMovID());
		m_nCursor++;
	}
	if (m_pDI->KeyPressed(DIK_RETURN) || m_pDI->JoystickButtonPressed(0,0) == true )
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetConfirmID());
		if (m_nCursor == 0)
		{
			CEntityManager::GetInstance()->Clear();
			CLevelManager::GetInstance()->LoadLevels();
			CLevelManager::GetInstance()->SetCurrLevel(0);
			CSaveSystem::GetInstance()->LoadSlot(1);
			CGame::GetInstance()->ChangeState(CSinglePlayerState::GetInstance());
		}
		if (m_nCursor == 1)
		{
			CEntityManager::GetInstance()->Clear();
			CLevelManager::GetInstance()->LoadLevels();
			CLevelManager::GetInstance()->SetCurrLevel(0);
			CSaveSystem::GetInstance()->LoadSlot(2);
			CGame::GetInstance()->ChangeState(CSinglePlayerState::GetInstance());
		}
		if (m_nCursor == 2)
		{
			CEntityManager::GetInstance()->Clear();
			CLevelManager::GetInstance()->LoadLevels();
			CLevelManager::GetInstance()->SetCurrLevel(0);
			CSaveSystem::GetInstance()->LoadSlot(3);
			CGame::GetInstance()->ChangeState(CSinglePlayerState::GetInstance());
		}
	}

	if( m_pDI->KeyPressed( DIK_ESCAPE ) == true || m_pDI->JoystickButtonPressed(7,0) )
	{
		CGame::GetInstance()->ChangeState(CPauseMenuState::GetInstance());
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetConfirmID());
	}

	return true;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Update
// Paramater	: fElapsedTime - The ammount of time passed since last update
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CLoadMenuState::Update( float fElapsedTime )
{
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CLoadMenuState::Render( void )
{
	CSGD_TextureManager::GetInstance()->Draw(m_nBackgroundImg, 0,0, 1.0f,1.0f);

	CBitmapFont* font = CGame::GetInstance()->GetFont();

	font->Draw( _T(">"), 50, 100 + (100 * m_nCursor), 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	font->Draw(Slot, 100, 100, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	font->Draw( _T("1"), 200 + CGame::GetInstance()->GetFont()->GetStringWidth(TrueSlot,1.0f), 100, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

	font->Draw(Slot, 100, 200, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	font->Draw( _T("2"), 200 + CGame::GetInstance()->GetFont()->GetStringWidth(TrueSlot,1.0f), 200, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

	font->Draw(Slot, 100, 300, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	font->Draw( _T("3"), 200 + CGame::GetInstance()->GetFont()->GetStringWidth(TrueSlot,1.0f), 300, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

	for (int i = 0; i < 3; i++)
	{
		wostringstream out;

		if (m_TimeArr[i]%60 < 10)
			out << Time << m_TimeArr[i]/60 << ":0" << m_TimeArr[i]%60;
		else 
			out << Time << m_TimeArr[i]/60 << ":" << m_TimeArr[i]%60;

		font->Draw( out.str().c_str(), 125, 132 + (i*100), 0.8f, 
			D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	}

	font->Draw( EscToMain, CGame::GetInstance()->GetScreenMidPoint(TrueEscTMain, 1.0f), 
		568,1.0f, D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

}